package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.protocols.*;

public class AspIdentifier implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(17,2); // ASP Identifier
	private OctaveInteger ParameterLength= new OctaveInteger(8,2);	// Length=8
	private OctaveInteger AspIdentifier= new OctaveInteger(4);
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	
	
	public AspIdentifier(Boolean ismandatory) {
	Mandatory=ismandatory;
	}
	
	public AspIdentifier(int aspidentifier,Boolean mandatory) {		
		AspIdentifier.Value=aspidentifier;
		Mandatory=mandatory;
		Visible=true;

	}

	
	public byte [] getByteArray(){		

		return  !Visible?new byte[0]:ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), AspIdentifier.getByteArray());
		

	}
	

	public int getParameterTag() {
		return ParameterTag.Value;
	}


	
	public int getLength() {
		return getByteArray().length;
	}



	public int getAspIdentifier() {
		return AspIdentifier.Value;
	}


	public void setAspIdentifier(int Identifier) {
		AspIdentifier.Value = Identifier;
		Visible=true;
	}
	public Boolean getVisible() {
		return Visible;
	}

	public void setVisible(Boolean visible) {
		Visible = visible;
	}
	

	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<8 && input.length>0)
			throw new ProtocolStackException("Decode Failure:AspIdentifier"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==17 && input[2]==0 && input[3]==8)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setAspIdentifier( ProtoOperations.intFromByteArray(input, 4,  ParameterLength.Value-4));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode Failure:AspIdentifier"+ProtoOperations.getHexString(input));
		else
			return input;
				
	}

	public Boolean isMandatory() {
		return Mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		Mandatory = mandatory;
	}
	

	
	
	
}
